﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SanguoshaCardsCounter
{
    class Cards
    {
        private Label Type;
        private NumericUpDown Used;
        private Label Left;
        private Label LastLeft;
        private NumericUpDown[] Players;

        private int CardTotal;
        private int CatdTotalEX;
        private int Total;

        public Cards(string cardType, int cardTotal, int cardTotalEX, TableLayoutPanel panel, int row)
        {
            CardTotal = cardTotal;
            CatdTotalEX = cardTotalEX;

            Type = new Label();
            Used = new NumericUpDown();
            Left = new Label();
            LastLeft = new Label();
            Players = new NumericUpDown[8];
            for (int i = 0; i < 8; ++i)
                Players[i] = new NumericUpDown();

            //panel
            panel.Controls.Add(Type, 0, row);
            panel.Controls.Add(Used, 1, row);
            panel.Controls.Add(Left, 2, row);
            panel.Controls.Add(LastLeft, 4, row);
            for (int i = 0; i < 8; ++i)
                panel.Controls.Add(Players[i], 5 + i, row);

            //type
            Type.Anchor = AnchorStyles.None;
            Type.AutoSize = true;
            Type.Text = cardType;

            //used
            Used.Anchor = AnchorStyles.None;
            Used.Minimum = Used.Maximum = 0;
            Used.ValueChanged += used_ValueChanged;

            //left
            Left.Anchor = AnchorStyles.None;
            Left.AutoSize = true;
            Left.Text = "0";

            //lastLeft
            LastLeft.Anchor = AnchorStyles.None;
            LastLeft.AutoSize = true;
            LastLeft.Text = "0";

            //players
            for (int i = 0; i < 8; ++i)
            {
                Players[i].Anchor = AnchorStyles.None;
                Players[i].Minimum = Players[i].Maximum = 0;
                Players[i].TabStop = false;
            }
        }

        void used_ValueChanged(object sender, EventArgs e)
        {
            Left.Text = (Total - Used.Value).ToString();
        }

        public void init(bool isEX, bool isEXChanged)
        {
            if (isEX)
                Total = CatdTotalEX;
            else
                Total = CardTotal;
            LastLeft.Text = Left.Text;
            if (!isEXChanged)
                Used.Value = 0;
            Used.Maximum = Total;
            Left.Text = (Total - Used.Value).ToString();
        }
    }
}
